package com.yy.bolg.service.impl;

import com.yy.bolg.dao.WorkOrderDao;
import com.yy.bolg.entity.WorkOrderBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.exception.OtherThingsException;
import com.yy.bolg.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单Service实现类，对接口里的方法进行构造填充
 * @author enmaoFu
 * @date 2024-11-11
 */
@Service("workOrderService")
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private WorkOrderDao workOrderDao;

    @Override
    public void add(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackCode())){
            throw new LackParameterException("上报编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerCode())){
            throw new LackParameterException("客户（单位）编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerName())){
            throw new LackParameterException("客户（单位）名称不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerUserName())){
            throw new LackParameterException("客户（单位）管理人姓名不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsCustomerUserPhone())){
            throw new LackParameterException("客户（单位）管理人手机号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsAdminUserCode())){
            throw new LackParameterException("总工编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsWorkOrderContent())){
            throw new LackParameterException("上报文字描述不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsWorkOrderImgFile())){
            throw new LackParameterException("上报图片描述不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackDate())){
            throw new LackParameterException("上报时间不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackProvince()) || StringUtils.isEmpty(bean.getPsFeedbackCity()) || StringUtils.isEmpty(bean.getPsFeedbackRegion())){
            throw new LackParameterException("客户上报地区不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsFeedbackAddress())){
            throw new LackParameterException("上报详细地址不能为空");
        }

        if(bean.getPsFeedbackLongitude() <= 0){
            throw new LackParameterException("客户上报经度不合法");
        }

        if(bean.getPsFeedbackLatitude() <= 0){
            throw new LackParameterException("客户上报纬度不合法");
        }

        if(bean.getPsFeedbackType() != 1 && bean.getPsFeedbackType() != 2){
            throw new LackParameterException("上报类型不合法");
        }

        try {
            workOrderDao.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("添加数据失败");
        }
    }

    @Override
    public List<WorkOrderBean> findAllByCondition(WorkOrderBean bean) throws Exception {
        if(bean.getPsWorkOrderState() > 6){
            throw new LackParameterException("工单状态不合法");
        }

        if(bean.getPsFeedbackType() < 1  && bean.getPsFeedbackType() > 3){
            throw new LackParameterException("上报类型不合法");
        }

        try {
            return workOrderDao.findAllByCondition(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<WorkOrderBean> findAllToIndex(WorkOrderBean bean) throws Exception {
        try {
            return workOrderDao.findAllToIndex(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public WorkOrderBean findOneByCode(WorkOrderBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsWorkOrderCode())) {
            throw new LackParameterException("工单编号不能为空");
        }

        try {
            return workOrderDao.findOneByCode(bean.getPsWorkOrderCode());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void dispatch(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(bean.getPsWorkOrderType() != 1 && bean.getPsWorkOrderType() != 2){
            throw new LackParameterException("工单类型不合法");
        }

        if(StringUtils.isEmpty(bean.getPsWorkOrderEffectDate())){
            throw new LackParameterException("工单派遣/发布时间不能为空");
        }

        if(bean.getPsWorkOrderRequirementDate() < 6 && bean.getPsWorkOrderRequirementDate() > 12){
            throw new LackParameterException("工单要求响应时间（小时数）不合法");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianCode())){
            throw new LackParameterException("电工编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianName())){
            throw new LackParameterException("电工姓名不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianPhone())){
            throw new LackParameterException("电工手机号不能为空");
        }

        if(bean.getPsWorkOrderState() < 0 && bean.getPsWorkOrderState() > 5){
            throw new LackParameterException("工单状态不合法");
        }

        try {
            workOrderDao.dispatch(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void publish(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(bean.getPsWorkOrderType() != 1 && bean.getPsWorkOrderType() != 2){
            throw new LackParameterException("工单类型不合法");
        }

        if(StringUtils.isEmpty(bean.getPsWorkOrderEffectDate())){
            throw new LackParameterException("工单派遣/发布时间不能为空");
        }

        if(bean.getPsWorkOrderRequirementDate() < 6 && bean.getPsWorkOrderRequirementDate() > 12){
            throw new LackParameterException("工单要求响应时间（小时数）不合法");
        }

        if(bean.getPsWorkOrderState() < 0 && bean.getPsWorkOrderState() > 5){
            throw new LackParameterException("工单状态不合法");
        }

        try {
            workOrderDao.publish(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void arrive(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianCode())){
            throw new LackParameterException("电工编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsWorkOrderArriveProvince()) || StringUtils.isEmpty(bean.getPsWorkOrderArriveCity()) || StringUtils.isEmpty(bean.getPsWorkOrderArriveRegion())){
            throw new LackParameterException("打卡地区不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsWorkOrderArriveAddress())){
            throw new LackParameterException("打卡详细地址不能为空");
        }

        if(bean.getPsWorkOrderArriveLongitude() <= 0){
            throw new LackParameterException("打卡经度不合法");
        }

        if(bean.getPsWorkOrderArriveLatitude() <= 0){
            throw new LackParameterException("打卡纬度不合法");
        }

        if(StringUtils.isEmpty(bean.getPsWorkOrderArriveImg())){
            throw new LackParameterException("打卡图片不能为空");
        }

        try {
            workOrderDao.arrive(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void accept(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianCode())){
            throw new LackParameterException("电工编号不能为空");
        }

        try {
            workOrderDao.accept(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void grabOrder(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianCode())){
            throw new LackParameterException("电工编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianName())){
            throw new LackParameterException("电工姓名不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianPhone())){
            throw new LackParameterException("电工手机号不能为空");
        }

        try {
            workOrderDao.grabOrder(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void refuse(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            workOrderDao.refuse(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void workAddTesting(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(StringUtils.isEmpty(bean.getPsTestingCode())){
            throw new LackParameterException("检测编号不能为空");
        }

        try {
            workOrderDao.workAddTesting(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void workCompleteTesting(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            workOrderDao.workCompleteTesting(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void workCompleteRepair(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        if(StringUtils.isEmpty(bean.getPsRepairCode())){
            throw new LackParameterException("维修编号不能为空");
        }

        try {
            workOrderDao.workCompleteRepair(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void endWorkOrder(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            workOrderDao.endWorkOrder(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public void doItAgainWorkOrder(WorkOrderBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsWorkOrderCode())){
            throw new LackParameterException("工单编号不能为空");
        }

        if (null == workOrderDao.findOneByCode(bean.getPsWorkOrderCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            workOrderDao.doItAgainWorkOrder(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public List<WorkOrderBean> findUnread(WorkOrderBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianCode())) {
            throw new LackParameterException("电工编号不能为空");
        }

        try {
            return workOrderDao.findUnread(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public void upUnread(WorkOrderBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianCode())) {
            throw new LackParameterException("电工编号不能为空");
        }

        try {
            workOrderDao.upUnread(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public void upAppointUnread(WorkOrderBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsWorkOrderCode())) {
            throw new LackParameterException("工单编号不能为空");
        }

        if (StringUtils.isEmpty(bean.getPsElectricianCode())) {
            throw new LackParameterException("电工编号不能为空");
        }

        try {
            workOrderDao.upAppointUnread(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

}
